package gov.fnal.xpathsearch.rest;

import gov.fnal.xpath2ldap.XPathLDAPEvaluator;
import gov.fnal.xpath2ldap.XPathSearch;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path("/browse")
public class BrowseResource {
   @Context UriInfo uriInfo;
   //@Context Request request;
   XPathQuery query = new XPathQuery();

	@GET
	//@Path("/{class1}/{class2}/{class3}/{class4}/{class5}/{class6}/{class7}/{class8}/{class9}/{class10}/{class11}/{class12}")
	@Path("/{class1}")
	//@Produces("text/plain")
	public String getAny(
			@PathParam("class1") String dir1/*,
			@PathParam("class2") String dir2,
			@PathParam("class3") String dir3,
			@PathParam("class4") String dir4,
			@PathParam("class5") String dir5,
			@PathParam("class6") String dir6,
			@PathParam("class7") String dir7,
			@PathParam("class8") String dir8,
			@PathParam("class9") String dir9,
			@PathParam("class10") String dir10,
			@PathParam("class11") String dir11,
			@PathParam("class12") String dir12*/,
			@QueryParam("base") String base) {
		if(base!=null) {
			System.out.println("base="+base);
			query.setLDAPBase(base);	
		}
		String path = uriInfo.getAbsolutePath().toString();
		query.setExpr("//" + dir1);
		String cli_str = query.toCliParseable();
        System.out.println(cli_str + "\n");
		String[] inarr = query.toArray();
		String result = XPathSearch.processXPathExpression(inarr);
		String htmlResult = listResultsHTML(path, result);
		System.out.println(htmlResult);
		return htmlResult;
	}
	
	@GET
	//@Path("/{class1}/{class2}/{class3}/{class4}/{class5}/{class6}/{class7}/{class8}/{class9}/{class10}/{class11}/{class12}")
	@Path("/{class1}/{class2}")
	public String getAny(
			@PathParam("class1") String dir1,
			@PathParam("class2") String dir2/*,
			@PathParam("class3") String dir3,
			@PathParam("class4") String dir4,
			@PathParam("class5") String dir5,
			@PathParam("class6") String dir6,
			@PathParam("class7") String dir7,
			@PathParam("class8") String dir8,
			@PathParam("class9") String dir9,
			@PathParam("class10") String dir10,
			@PathParam("class11") String dir11,
			@PathParam("class12") String dir12*/,
			@QueryParam("base") String base) {
		if(base!=null) {
			System.out.println("base="+base);
			query.setLDAPBase(base);	
		}
		String path = uriInfo.getAbsolutePath().toString();
		query.setExpr("//" + dir1);
		String cli_str = query.toCliParseable();
        System.out.println(cli_str + "\n");
		String[] inarr = query.toArray();
		String result = XPathSearch.processXPathExpression(inarr);
		String htmlResult = listResultsHTML(path, result);
		System.out.println(htmlResult);
		return htmlResult;
	}
	
	public String listResultsHTML(String parent, String list) {
		String[] results = list.split("\n");
	    StringBuilder  listTmpl = new StringBuilder();
	    listTmpl.append("<html><head><title>");
	    listTmpl.append(parent);
	    listTmpl.append("</title></head><body>\n");
	    listTmpl.append("<h2>");
	    listTmpl.append(parent);
	    listTmpl.append("</h2>\n");
	    listTmpl.append("<ul>\n");
	    listTmpl.append("<li><a href=\"../\">..</a></li>\n");
	    for (String res : results) {
	    	listTmpl.append("<li><a href=\"");
	    	listTmpl.append(parent);
	    	listTmpl.append("/*?base=");
	    	listTmpl.append(res);
	    	listTmpl.append("\">");
	    	listTmpl.append(res);
	    	listTmpl.append("</a></li>\n");
	    }
	    listTmpl.append("</ul>\n");
	    listTmpl.append("</body></html>");
		return listTmpl.toString();	
	}
    
}
